package webEquipeRechercheGwt.client.entityShowingPanel.linkCreationPanel.seminar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import webEquipeRechercheGwt.client.callback.consultation.CallBackGetAllPerson;
import webEquipeRechercheGwt.client.callback.consultation.commands.CommandGetAllPersonsImplToPersonLinkPanel;
import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityShowingPanel.SeminarPanel;
import webEquipeRechercheGwt.client.entityShowingPanel.linkCreationPanel.ToPersonLinkCreationPanel;
import webEquipeRechercheGwt.client.handler.creation.link.seminar.ClickHandlerSeminarToPersonLink;
import webEquipeRechercheGwt.client.services.PersonService;
import webEquipeRechercheGwt.client.services.PersonServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;

public class SeminarToPersonLinkCreationPanel extends HorizontalPanel implements ToPersonLinkCreationPanel {

	private SeminarPanel origine;
	private List<PersonMirror> persons;
	private ListBox listBox;
	private Button validation;
	
	public SeminarToPersonLinkCreationPanel(SeminarPanel origine){
		super();
		this.origine = origine;
		persons = new ArrayList<PersonMirror>();
		listBox = new ListBox();
		listBox.setVisibleItemCount(1);
		validation = new Button("Create link");
		validation.addClickHandler(new ClickHandlerSeminarToPersonLink(persons, listBox, origine.getMirror(), origine.getMainPanel()));
	}
	
	private void loadAllPersons(){
		PersonServiceAsync service = GWT.create(PersonService.class);
		AsyncCallback<Collection<PersonMirror>> callback = new CallBackGetAllPerson(new CommandGetAllPersonsImplToPersonLinkPanel(this));
		service.getPersons(callback);
	}
	
	public void setPersons(Collection<PersonMirror> result){
		persons.clear();
		persons.addAll(result);
		persons.removeAll(origine.getMirror().getSpeakers());
		updateSelection();
	}
	
	private void updateSelection(){
		listBox.clear();
		for(PersonMirror p : persons){
			listBox.addItem(p.getName()+" "+p.getFirstname());
		}
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		add(listBox);
		add(validation);
		loadAllPersons();
	}
	
	
}
